package cn.org.timespace.easyframe.util.jasper;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import cn.org.timespace.easyframe.util.EfResourceContext;
import cn.org.timespace.easyframe.util.ds.EfDataSourceUtil;

/**
 * <p>
 * 报表生成的抽象实现类
 * </p>
 * 
 * @version 1.0, 11/01/18
 * @since 1.0
 * @author 
 */

public abstract class EfJasperReportAbstractReport {
	
	private static final String JASPER_FILE_ROOT_DIR = "JasperFileRootDir";
	private static final String EXPORT_FILE_TYPE = "fileType";
	public static final String JASPER_FILE_SUFFIX = ".jasper";
	
	private String jasperFileRootDir;
	
	protected File jasperFile;
	
	protected Map<Object, Object> jasperParams;

	
	protected EfJasperReportAbstractReport() {
		jasperParams = new HashMap<Object, Object>(5);
	}

	protected Connection getConnection() {
		return EfDataSourceUtil.getInstance().getConnection();
	}

	public void view(ServletRequest request, ServletResponse response) {
		createJasperParam(request);
		jasperFile = new File(request.getRealPath(getJapserReportFilePath()));

		Connection connection = null;
		try {

			connection = getConnection();

			EfJasperReportUtil.defaultDirectReport(jasperFile, jasperParams,
					connection, (HttpServletResponse) response,EfJasperReportUtil.PDF_FILE);
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			if(connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
	}

	public void export(ServletRequest request, ServletResponse response) {

		int fileType = 2;
		try {
			fileType = Integer.parseInt((String) request.getParameter(EXPORT_FILE_TYPE));
		} catch (Exception e) {
			fileType = 2;
		}

		createJasperParam(request);
		jasperFile = new File(request.getRealPath(getJapserReportFilePath()));
		
		Connection connection = null;
		try {

			connection = getConnection();

			EfJasperReportUtil.defaultExportReport(jasperFile, jasperParams,
					connection, fileType, getReportExportName(),
					(HttpServletResponse) response);
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			if(connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
	}

	protected void addJasperParams(Object key, Object value) {
		jasperParams.put(key, value);
	}

	protected String getCurrentDateString(String dateFormat) {
		if (null == dateFormat || "".equals(dateFormat)) {
			dateFormat = "yyyy-MM-dd";
		}
		return new SimpleDateFormat(dateFormat).format(new Date());
	}

	protected abstract void createJasperParam(ServletRequest request);

	protected abstract String getJapserReportFilePath();

	protected abstract String getReportExportName();

	public String getJasperFileRootDir() {
		this.jasperFileRootDir = EfResourceContext.getInstance().getParamValue(JASPER_FILE_ROOT_DIR);
		return jasperFileRootDir;
	}

	public void setJasperFileRootDir(String jasperFileRootDir) {
		this.jasperFileRootDir = jasperFileRootDir;
	}
	
}
